# generate a simulated community
# 
# Author: Guochun Shen
# Data:   2012-1-6
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

rcommunity=function(com_control){

	#basic information
	N=com_control$N
	S=com_control$S
	win=com_control$win
	area=com_control$win$xrange[2]*com_control$win$yrange[2]
	#total intensity
	if(com_control$ab=="equal")
		N_intensity=rep(N/S,S)/area
	else if(com_control$ab=="logseries"){
		N_intensity=rlogseries(N,S)/area
	}
	#species name
	spnames=1:com_control$S
	niche=matrix(c(runif(S,0,1)),nrow=S,ncol=1)
	
	#generate covariable
	if(com_control$covr){
		trend=list()
		covr=sin(seq(0,win$xrange[2])/10)
		cl=length(seq(0,win$xrange[2]))
		covr=matrix(rep(covr,each=length(covr)),ncol=cl,nrow=cl)
		for(i in 1:S){
			tri=exp(abs(covr-niche[i]))
			mu=log(N_intensity[i]/sum(exp(tri)))
			trend[[i]]=im(tri+mu,xrange=win$xrange,yrange=win$yrange)
		}
	}
	
	#competition
	if(com_control$inter!="independent"){
		#intensity of each type
		beta=rep(com_control$beta,S)
		#strength of interaction
		gamma=as.matrix(dist(niche))
		gamma=round(gamma/max(gamma),2)
		gamma=sqrt(gamma+0.5)
		gamma=gamma/max(gamma)
		#diag(gamma)=0.00
		#gamma=gamma[S:1,]
		rownames(gamma)=colnames(gamma)=1:S
		#radio of interaction
		r=matrix(com_control$r,nrow=S,ncol=S)
	}
	
	#TODO 
	
	
}

